import { getOrderList } from '../api/index';
import { selectOrderOptions, statusOptions, genderOptions } from '../utils/enumType';

Page({
  data: {
    active: {
      type: 0,
      status: 0,
      location: 0,
      gender: 0
    },
    formData: {
      type: '',
      status: '',
      location: '',
      gender: '',
    },
    total: 0,
    orderList: [],
    serviceOptions: selectOrderOptions,
    statusOptions: statusOptions,
    locationOptions: [
      { text: '全部地点', value: 0 },
      { text: '地点1', value: 1 },
      { text: '地点2', value: 2 }
    ],
    genderOptions: genderOptions
  },
  onChange(e) {
    const { type } = e.currentTarget.dataset;
    const value = e.detail;
    
    const optionsMap = {
      'type': 'serviceOptions',
      'status': 'statusOptions',
      'location': 'locationOptions',
      'gender': 'genderOptions'
    };
    
    const options = this.data[optionsMap[type]];
    const selectedOption = options[value];
    const actualValue = selectedOption.text === '全部' || selectedOption.text === '全部地点' ? '' : selectedOption.text;
    
    this.setData({
      [`active.${type}`]: value,
      [`formData.${type}`]: actualValue
    });
    
    this.getOrderList({
      [type]: actualValue
    });
  },

  onShow() {
    this.setData({
      formData: {
        ...this.data.formData,
      }
    });
    this.getOrderList();

  },
  getOrderList(params = {}) {
    // wx.showLoading({
    //   title: '加载中',
    // });
    
    getOrderList({
      ...this.data.formData, 
      ...params
    }).then(res => {
      this.setData({
        orderList: res.result,
      });
    }).finally(() => {
      // wx.hideLoading();
    });
  }

});
